<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
var menuPermissionTree;
var roleId = $.windowLocationSearch()["id"];

function saveMenuPermission() {
	var result = [];
	
	menuPermissionTree.jstree("get_checked").each(function(i,item) {
		var data = {"checked":true,"id":$(item).data("id"),"permissionId":$(item).data("permissionId")};
		result.push(data);
	});
	
	menuPermissionTree.jstree("get_unchecked").each(function(i,item) {
		var data = {"checked":false,"id":$(item).data("id"),"permissionId":$(item).data("permissionId")};
		result.push(data);
	});
	
	doSaveMenuPermission($.toJSON(result));
}

function doSaveMenuPermission(data) {
	var params = {
			roleId : roleId,
			data : data
		};
	
	$.ajax({
		type: "POST",
		url: ctx + "/biz/system/role/permission/save.json",
		data: params,
		success: saveSucceedCallback,
		dataType:"json",
		error:function(message){
			$.alert(message);
		}
	});
}

function saveSucceedCallback(data) {
	if (!data.succeed) {
		$.notice(data.message);
		return;
	}

	$(data.data).each(function() {
		var _this = this;
		menuPermissionTree.find("li").each(function() {
			if ($(this).data("id") == _this.id) {
				$(this).data("permissionId",_this.permissionId);
			}	
		});		
	});
 
	$.info("已保存");
}

function initMenuPermission() {
	menuPermissionTree = $.readonlyTree(
			"#menuPermissionTreeDiv",
			ctx + "/biz/system/menu/permission/children.json?roleId="+roleId,
			{
				is_selected:function(d) { //extended by cms
					if (d != null) {
						return d.data("permissionId") != null && d.data("permissionId") != "null";
					}
					return false;
				}
			});
};

</script>

<form  class="form-horizontal">
	<div class="control-group">
		<label class="control-label">菜单：</label>
		<div class="controls">
			<div id="menuPermissionTreeDiv" class="well"></div>
		</div>
	</div>
	<div class="form-actions">
		<a href="#" class="btn btn-primary" onclick="saveMenuPermission()"><i class="icon-ok"></i>保存</a>
	</div>
</form>